#!/bin/sh

zero_enable=$(uci get zerotier.sample_config.enabled)

if [ $zero_enable -eq 1 ]; then

if [ -f /tmp/zero.log ];then
while [ "$(ifconfig | grep zt | awk '{print $1}')" = "" ]  
do 
   sleep 1
done
fi

nat_enable=$(uci get zerotier.sample_config.nat)
zt0=$(ifconfig | grep zt | awk '{print $1}')
echo "zt interface $zt0 is started!"
echo $zt0 > /tmp/zt.nif
iptables -D FORWARD -i $zt0 -j ACCEPT 2>/dev/null
iptables -D FORWARD -o $zt0 -j ACCEPT 2>/dev/null
iptables -t nat -D POSTROUTING -o $zt0 -j MASQUERADE 2>/dev/null
if [ $nat_enable -eq 1 ]; then
  iptables -I FORWARD -i $zt0 -j ACCEPT
  iptables -I FORWARD -o $zt0 -j ACCEPT
  iptables -t nat -I POSTROUTING -o $zt0 -j MASQUERADE
  ip_segment=$(ip route | grep "dev $zt0 proto" | awk '{print $1}')
  iptables -t nat -I POSTROUTING -s $ip_segment -j MASQUERADE
fi

fi
